package ai4games_tests.maths;

import game2dai.maths.Geometry2D;
import processing.core.PApplet;

public class LineBoxIntersection extends PApplet {
	final int IRAD = 5;
	float ax0, ay0, ax1, ay1, bx0, by0, bx1, by1;
	float bw = 50, bh = 30;
	
	float[] ipoint = null;
	boolean intersect;
	
	public void setup(){
		size(400,300);
		cursor(CROSS);
		float cx = width/2;
		float cy = height/2;
		float w = 0.15f * width;
		float h = 0.15f * height;
		ax0 = cx - w;
		ay0 = cy - h;
		ax1 = cx + w;
		ay1 = cy + h;
		
		intersect = false;
		rectMode(CORNERS);
	}

	public void mousePressed(){
		bx0 = bx1 = mouseX;
		by0 = by1 = mouseY;		
	}
	
	public void mouseDragged(){
		bx1 = mouseX;
		by1 = mouseY;
	}
	
	public void draw(){
		background(128);
		
		intersect = Geometry2D.line_box_xywh(bx0, by0, bx1, by1, ax0, ay0, ax1 - ax0, ay1 - ay0);
	//	intersect = Geometry2D.line_box_xyxy(bx0, by0, bx1, by1, ax0, ay0, ax1, ay1);	
		
		strokeWeight(1.1f);
		stroke(0,255,0);
		fill(255,255,192,140);
		if(intersect)
			stroke(255,0,0);
		rect(ax0, ay0, ax1, ay1);
		line(bx0, by0, bx1, by1);
	}

	public float[] makeFloatArray(double[] da){
		float[] fa = new float[da.length];
		for(int i = 0; i < fa.length; i++)
			fa[i] = (float)da[i];
		return fa;
	}
}
